package com.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjWorkGoodsCode;
import com.zb.mapper.ZjWorkGoodsCodeMapper;
import com.zb.service.ZjWorkGoodsCodeService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 工单商品编码表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-09-20
 */
@Service
public class ZjWorkGoodsCodeServiceImpl extends ServiceImpl<ZjWorkGoodsCodeMapper, ZjWorkGoodsCode> implements ZjWorkGoodsCodeService {

    // 根据工单商品表ID批量删除
    @Override
    public void deleteBatchByWorkGoodsIds(List<Long> workGoodsIds) {
        if (null == workGoodsIds || workGoodsIds.isEmpty()) {
            return;
        }
        QueryWrapper<ZjWorkGoodsCode> wrapper = new QueryWrapper<>();
        wrapper.in("work_goods_id", workGoodsIds);
        this.remove(wrapper);
    }

    // 根据工单商品表ID获取信息
    @Override
    public List<ZjWorkGoodsCode> getByWorkGoodsIds(List<Long> workGoodsIds) {
        if (CollectionUtil.isEmpty(workGoodsIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjWorkGoodsCode> wrapper = new QueryWrapper<>();
        wrapper.select("id", "work_goods_id", "code_id", "code");
        wrapper.in("work_goods_id", workGoodsIds);
        return this.list(wrapper);
    }

    // 根据工单商品表ID获取信息
    @Override
    public List<ZjWorkGoodsCode> getByWorkGoodsId(Long workGoodsId) {
        QueryWrapper<ZjWorkGoodsCode> wrapper = new QueryWrapper<>();
        wrapper.select("id", "work_goods_id", "code_id", "code");
        wrapper.eq("work_goods_id", workGoodsId);
        return this.list(wrapper);
    }

    // 根据编码ID查询关联的编码信息
    @Override
    public List<Long> getRelationCodeIdByCodeIds(List<Long> codeIds) {
        if (null == codeIds || codeIds.isEmpty()) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjWorkGoodsCode> wrapper = new QueryWrapper<>();
        wrapper.select("code_id");
        wrapper.in("code_id", codeIds);
        return this.list(wrapper).stream().map(ZjWorkGoodsCode::getCodeId).distinct().collect(Collectors.toList());
    }

    // 根据编码ID获取工单ID集合
    @Override
    public List<Long> getWorkIdsByCodeId(Long codeId) {
        QueryWrapper<ZjWorkGoodsCode> wrapper = new QueryWrapper<>();
        wrapper.select("work_id");
        wrapper.eq("code_id", codeId);
        return this.list(wrapper).stream().map(ZjWorkGoodsCode::getWorkId).distinct().collect(Collectors.toList());
    }

    // 根据商品防伪编码获取工单ID
    @Override
    public List<Long> getWorkIdByCode(Long custAccId, String code) {
        QueryWrapper<ZjWorkGoodsCode> wrapper = new QueryWrapper<>();
        wrapper.select("work_id");
        wrapper.eq("cust_acc_id", custAccId);
        wrapper.like("code", code);
        wrapper.last("LIMIT 100");
        return this.list(wrapper).stream().map(ZjWorkGoodsCode::getWorkId).distinct().collect(Collectors.toList());
    }
}
